/*
 * Copyright (c) 2013-2015 Charkey. All rights reserved.
 *
 * This software is the confidential and proprietary information of Charkey.
 * You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the agreements you entered into with Charkey.
 *
 * Charkey MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
 *
 * Charkey SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
 * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

package cn.simastudio.talos.core.service.base.impl;

import cn.simastudio.talos.core.service.base.ClientService;
import cn.simastudio.talos.core.service.base.OAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;

/**
 * <p>User: Zhang Kaitao
 * <p>Date: 14-2-17
 * <p>Version: 1.0
 */
@SuppressWarnings("SpringJavaAutowiringInspection")
@Service
public class OAuthServiceImpl implements OAuthService {

    private Cache authCodeCache;

    private Cache accessTokenCache;

    private Cache refreshTokenCache;

    @Autowired
    private ClientService clientService;

    @Autowired
    public OAuthServiceImpl(CacheManager cacheManager) {
        this.authCodeCache = cacheManager.getCache("auth_code_cache");
        this.accessTokenCache = cacheManager.getCache("access_token_cache");
        this.refreshTokenCache = cacheManager.getCache("refresh_token_cache");
    }

    public OAuthServiceImpl() {
    }

    @Override
    public void addAuthCode(String authCode, String credential) {
        authCodeCache.put(authCode, credential);
    }

    @Override
    public void addAccessToken(String accessToken, String credential) {
        accessTokenCache.put(accessToken, credential);
    }

    @Override
    public void addRefreshToken(String refreshToken, String[] credentialAndAccessToken) {
        refreshTokenCache.put(refreshToken, credentialAndAccessToken);
    }

    @Override
    public boolean checkAuthCode(String authCode) {
        return authCodeCache.get(authCode) != null;
    }

    @Override
    public boolean checkAccessTokenExpired(String accessToken) {
        return accessTokenCache.get(accessToken) == null;
    }

    @Override
    public boolean checkRefreshToken(String refreshToken) {
        return refreshTokenCache.get(refreshToken) != null;
    }

    @Override
    public boolean checkClientId(String clientId) {
        return clientService.findByClientId(clientId) != null;
    }

    @Override
    public boolean checkClientSecret(String clientSecret) {
        return clientService.findByClientSecret(clientSecret) != null;
    }

    @Override
    public String getCredentialByAuthCode(String authCode) {
        Cache.ValueWrapper valueWrapper = authCodeCache.get(authCode);
        return valueWrapper == null ? "" : (String) valueWrapper.get();
    }

    @Override
    public String getCredentialByAccessToken(String accessToken) {
        Cache.ValueWrapper valueWrapper = accessTokenCache.get(accessToken);
        return valueWrapper == null ? "" : (String) valueWrapper.get();
    }

    @Override
    public String getCredentialByRefreshToken(String refreshToken) {
        Cache.ValueWrapper valueWrapper = refreshTokenCache.get(refreshToken);
        return valueWrapper == null ? "" : ((String[]) valueWrapper.get())[0];
    }

    @Override
    public String getAccessTokenByRefreshToken(String refreshToken) {
        Cache.ValueWrapper valueWrapper = refreshTokenCache.get(refreshToken);
        return valueWrapper == null ? "" : ((String[]) valueWrapper.get())[1];
    }

    @Override
    public long getAuthCodeExpireIn() {
        return 600L;
    }

    @Override
    public long getAccessTokenExpireIn() {
        return 3600L;
    }

    @Override
    public void evictAuthCode(String authCode) {
        authCodeCache.evict(authCode);
    }

    @Override
    public void evictRefreshToken(String refreshToken) {
        refreshTokenCache.evict(refreshToken);
    }

}
